package club.kingyin.kyblogprofile.controller;


import club.kingyin.commons.entity.file.Quote;
import club.kingyin.commons.response.Result;
import club.kingyin.easycache.annotation.EasyCache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import club.kingyin.kyblogprofile.service.QuoteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Quote)表控制层
 *
 * @author makejava
 * @since 2022-02-16 18:28:24
 */
@RestController
@RequestMapping("quote")
public class QuoteController {
    /**
     * 服务对象
     */
    @Resource
    private QuoteService quoteService;

//    /**
//     * 分页查询所有数据
//     *
//     * @param page  分页对象
//     * @param quote 查询实体
//     * @return 所有数据
//     */
//    @GetMapping
//    public Result selectAll(Page<Quote> page, Quote quote) {
//        return Result.success(this.quoteService.page(page, new QueryWrapper<>(quote)));
//    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @EasyCache(prams = {"id"}, module = "ky-file")
    @GetMapping("/path")
    public Result selectOne(@RequestParam Serializable id) {
        return Result.success(this.quoteService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param quote 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody Quote quote) {
        return Result.success(this.quoteService.save(quote));
    }

    /**
     * 修改数据
     *
     * @param quote 实体对象
     * @return 修改结果
     */
    @EasyCache(prams = {"quote.id"},delete = {"selectOne"},module = "ky-file")
    // 跨模块删除
    @EasyCache(prams = {"userid=quote.id"}, delete = {"getUserDetail"}, module = "ky-user")
    @PostMapping("/update")
    public Result update(@RequestBody Quote quote) {
        if (quote.getPath() == null || "".equals(quote.getPath())) {
            return Result.failN("无效修改");
        }
        return Result.success(this.quoteService.updateById(quote));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public Result delete(@RequestParam("idList") List<Long> idList) {
        return Result.success(this.quoteService.removeByIds(idList));
    }
}

